<template>
    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" v-model="open" width="680px" append-to-body @close="close">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="设备名称" prop="deviceName">
                <el-input v-model="form.deviceName" placeholder="请输入设备名称"/>
            </el-form-item>
            <el-form-item label="设备编号" prop="deviceCode">
                <el-input v-model="form.deviceCode" placeholder="请输入设备编号"/>
            </el-form-item>
            <el-form-item label="经度" prop="lng">
                <el-input-number v-model="form.lng" placeholder="请输入经度"/>
            </el-form-item>
            <el-form-item label="纬度" prop="lat">
                <el-input-number v-model="form.lat" placeholder="请输入纬度"/>
            </el-form-item>
            <el-form-item label="所属分类" prop="categoryId">
                <el-select v-model="form.categoryName" placeholder="请选择所属分类" clearable>
                    <el-option :value="categoryData" style="height: auto;">
                        <el-tree ref="deptTree"
                                 :data="categoryTreeData" :props="categoryTreeProps"
                                 :show-checkbox="false"
                                 @node-click="selectCategory">
                        </el-tree>
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="所属驱动" prop="driverId">
                <el-select v-model="form.driverId">
                    <el-option
                            v-for="driver in driverListData"
                            :key="driver.driverId"
                            :label="driver.driverName"
                            :value="driver.driverId"
                    ></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {addDevice, updateDevice} from '/src/api/device/manage'
    import {getDriverList} from '/src/api/driver/manage'

    export default {
        name: "update",
        emits: ['dialogStatusChanged'],
        props: {
            open: Boolean,
            title: String,
            form: Object,
            //分类树数据
            categoryTreeData: Array,
        },
        data() {
            return {
                driverListData: [],
                // 表单校验
                rules: {
                    deviceName: [
                        {required: true, message: "设备名称不能为空", trigger: "blur"}
                    ],
                    deviceCode: [
                        {required: true, message: "设备编号不能为空", trigger: "blur"}
                    ],
                    lng: [
                        {required: true, message: "经度不能为空", trigger: "blur"}
                    ],
                    lat: [
                        {required: true, message: "纬度不能为空", trigger: "blur"}
                    ],
                },
                //分类树属性
                categoryTreeProps: {
                    children: 'children',
                    label: 'categoryName'
                },
                categoryData: {},
            }
        },
        created() {
            this.getDriverListByParam()
        },
        computed: {
            //监测分类ID
            monitorCategoryId() {
                return this.open
            },
        },
        watch: {
            monitorCategoryId(newVal) {
                if (newVal && this.form.categoryId) {
                    //根据分类ID匹配部门信息
                    this.getCategoryName(this.categoryTreeData, this.form.categoryId)
                }
            }
        },
        methods: {
            getDriverListByParam() {
                getDriverList().then(response => {
                    this.driverListData = response.data
                })
            },
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.deviceId != undefined) {
                            updateDevice(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.close();
                            });
                        } else {
                            addDevice(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.close();
                            });
                        }
                    }
                })
            },
            //取消
            cancel() {
                this.$emit('dialogStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('dialogStatusChanged', false)
            },
            selectCategory(data, node, nodeData) {
                this.categoryData = data
                this.form.categoryName = data.categoryName
                this.form.categoryId = data.categoryId
            },
            //根据分类ID获取分类名称
            getCategoryName(categoryTree, categoryId) {
                for (let i = 0; i < categoryTree.length; i++) {
                    const categoryInfo = categoryTree[i]
                    if (categoryInfo.categoryId === categoryId) {
                        this.form.deptName = categoryInfo.categoryName
                    }
                    if (categoryInfo.children && categoryInfo.children.length > 0) {
                        this.getCategoryName(categoryInfo.children, categoryId)
                    }
                }
            }
        },
    }
</script>

<style scoped>

</style>
